package com.example.serverdemo.dto;

import cn.hutool.core.convert.Convert;
import com.example.serverdemo.bean.CentralControllerProtocolContentBean;
import com.example.serverdemo.bean.CentralControllerProtocolInfoBean;
import com.example.serverdemo.utils.log.PrintLogUtil;

import javax.swing.text.ComponentView;
import java.util.ArrayList;
import java.util.Arrays;

public class CentralControllerProtocolDto {
    private CentralControllerProtocolInfoBean info;
    private ArrayList<CentralControllerProtocolContentBean> contents;

    public CentralControllerProtocolInfoBean getInfo() {
        return info;
    }

    public void setInfo(CentralControllerProtocolInfoBean info) {
        this.info = info;
    }

    public ArrayList<CentralControllerProtocolContentBean> getContents() {
        return contents;
    }

    public void setContents(ArrayList<CentralControllerProtocolContentBean> contents) {
        this.contents = contents;
    }

    public byte[] toHexData(){
        try {
            String[] strBuff;
            byte[] hexBuff;
            byte[] data = new byte[256 * 8 * 8];
            Arrays.fill(data,Convert.intToByte(0x00));
            switch (info.getComZero()){
                case 9600:
                    data[0] = Convert.intToByte(0x01);
                    break;
                case 4800:
                    data[0] = Convert.intToByte(0x02);
                    break;
                case 1200:
                    data[0] = Convert.intToByte(0x03);
                    break;
                case 19200:
                default:
                    data[0] = Convert.intToByte(0x00);
                    break;

            }
            switch (info.getComOne()){
                case 9600:
                    data[1] = Convert.intToByte(0x01);
                    break;
                case 4800:
                    data[1] = Convert.intToByte(0x02);
                    break;
                case 1200:
                    data[1] = Convert.intToByte(0x03);
                    break;
                case 19200:
                default:
                    data[1] = Convert.intToByte(0x00);
                    break;

            }
            switch (info.getPsTwo()){
                case 0:
                    data[2] = Convert.intToByte(0x01);
                    break;
                case 1:
                default:
                    data[2] = Convert.intToByte(0x00);
                    break;
            }
            for (CentralControllerProtocolContentBean content : contents) {
                if (content.getComZeroUp() != null && content.getComZeroUp().length() > 0){
                    strBuff = content.getComZeroUp().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[0 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getComZeroDown() != null && content.getComZeroDown().length() > 0){
                  strBuff = content.getComZeroDown().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[1 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getComOneUp() != null && content.getComOneUp().length() > 0){
                    strBuff = content.getComOneUp().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[2 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getComOneDown() != null && content.getComOneDown().length() > 0){
                    strBuff = content.getComOneDown().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[3 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getInfraredSend() != null && content.getInfraredSend().length() > 0){
                    strBuff = content.getInfraredSend().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[4 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getInfraredReceive() != null && content.getInfraredReceive().length() > 0){
                    strBuff = content.getInfraredReceive().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[5 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getPsTwoUp() != null && content.getPsTwoUp().length() > 0){
                    strBuff = content.getPsTwoUp().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[6 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
                if (content.getPsTwoDown() != null && content.getPsTwoDown().length() > 0){
                    strBuff = content.getPsTwoDown().split(" ");
                    for (int valueIndex = 0;valueIndex < 8 && valueIndex < strBuff.length;valueIndex++){
                        data[7 * 2048 + content.getCode() * 8 + valueIndex] = Convert.hexToBytes(strBuff[valueIndex])[0];
                    }
                }
            }
            return data;
        }catch (Exception e){
            PrintLogUtil.printError(this,e);
        }
        return null;
    }
}
